package com.simon.mctm.biz.sys.controller;


import com.simon.mctm.biz.sys.bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.CollectionUtils;

import com.simon.mctm.base.response.DoResponse;
import com.simon.mctm.base.response.PageResponse;
import com.simon.mctm.exception.CheckException;
import com.simon.mctm.exception.OperationFailedException;
import com.simon.mctm.token.TokenValidate;
import com.simon.mctm.base.PageResult;
import com.simon.mctm.biz.sys.bean.Role;
import com.simon.mctm.biz.sys.service.RoleService;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import java.util.Date;
import java.util.List;


/**
 * <p>
 * 系统角色 前端控制器
 * </p>
 *
 * @author simon
 * @since 2018-06-15
 */
@RestController
@Api(description = "系统角色控制器")
@RequestMapping("/sys/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "添加系统角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "role", value = "系统角色对象", required = true, dataType = "Role")
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @TokenValidate
    public DoResponse<?> addRole(@RequestBody Role role){
        Role dRole = roleService.selectOne(
                new EntityWrapper<Role>().eq("name",role.getName())
        );
        if (dRole != null) {
            throw new OperationFailedException("该角色已存在");
        } else {
            boolean res = roleService.insert(role);
            if (res) {
                return new DoResponse<>();
            } else {
                throw new OperationFailedException("添加系统角色失败");
            }
        }
    }


    @ApiOperation(value = "获取指定系统角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "系统角色id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getRole(@RequestParam Integer id) {
        Role role = roleService.selectById(id);
        if (role != null) {
            return new DoResponse<>(role);
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "获取系统角色列表")
    @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getAllRole() {
        EntityWrapper<Role> ew = new EntityWrapper<>();
        List<Role> roleList = roleService.selectList(ew);
        if (!CollectionUtils.isEmpty(roleList)) {
            return new DoResponse<>(roleList);
        } else {
            throw new OperationFailedException();
        }
    }


    @ApiOperation(value = "查询系统角色列表,分页形式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "角色名称", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "pageNum", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int", defaultValue = "10")
    })
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @TokenValidate
    public PageResponse<?> getRolePagedList(@RequestParam String name,@RequestParam Integer pageNum,
                                            @RequestParam Integer pageSize) {
        if (pageNum == 0 || pageSize == 0) {
            throw new CheckException("页码和单页条目必输");
        }
        Page<Role> rolePage = roleService.selectPage(
                new Page<>(pageNum, pageSize),
                new EntityWrapper<Role>().like("name",name));
        List<Role> roleList = rolePage.getRecords();
        PageResult bPage = roleService.calPage(name,pageSize);
        if (!CollectionUtils.isEmpty(roleList)) {
            return new PageResponse<>(roleList, roleList.size(), bPage.getTotalElements(), bPage.getTotalPage());
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "删除系统角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "系统角色id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @TokenValidate
    public DoResponse<?> deleteRole(@RequestParam Integer id) {
        boolean res = roleService.deleteById(id);
        if (res) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "更新系统角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "系统角色id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "role", value = "系统角色对象", required = true, dataType = "Role")
    })
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @TokenValidate
    public DoResponse<?> updateRole(@RequestParam Integer id, @RequestBody Role role) {
        Role temp = roleService.selectById(id);
        if (temp == null) {
            throw new OperationFailedException("该系统角色不存在");
        }
        boolean res = roleService.updateById(role);
        if (res) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }


}

